﻿using B.S.Passport.ErrorCode;
using B.S.Passport.Read.API.Application.Command123.PurNameCommand;
using B.S.Passport.Read.API.Dto;
using DAL;
using MediatR;

namespace B.S.Passport.Read.API.Application.Commandhander123.PurNameCommandHander
{
    public class GetPurchaseCommandHander : IRequestHandler<GetPurchaseCommand, ApiResult<Apipage<pDto>>>
    {
        public readonly DBHelper1 dBHelper;

        public GetPurchaseCommandHander(DBHelper1 dBHelper)
        {
            this.dBHelper = dBHelper;
        }

        public Task<ApiResult<Apipage<pDto>>> Handle(GetPurchaseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<Apipage<pDto>> apiResult = new ApiResult<Apipage<pDto>>();
            apiResult.Code = (ApiEnums)Renum.显示成功;
            apiResult.Message = Renum.显示成功.ToString();

            try
            {
                string baseListSql = "SELECT count(*) FROM ProductModel A JOIN PurchaseOrderProduct B ON A.ProductId = B.ProductId JOIN PurchaseOrder C ON B.PurchaseOrderId = C.PurchaseOrderId ";
                string baseSql = "SELECT * FROM ProductModel A JOIN PurchaseOrderProduct B ON A.ProductId = B.ProductId JOIN PurchaseOrder C ON B.PurchaseOrderId = C.PurchaseOrderId ";

                List<string> conditions = new List<string>();

                // 新增 WHouseId 查询条件（int类型）
                //if (request.WHouseId != null)
                //{
                //    conditions.Add($"B.WHouseId = {request.WHouseId}");
                //}
                if (!string.IsNullOrEmpty(request.ProductName))
                {
                    conditions.Add($"A.ProductName LIKE '%{request.ProductName}%'");
                }
                if (!string.IsNullOrEmpty(request.PurchaseOrderNumber))
                {
                    conditions.Add($"C.PurchaseOrderNumber LIKE '%{request.PurchaseOrderNumber}%'");
                }
                if (!string.IsNullOrEmpty(request.ManufacturerCode))
                {
                    conditions.Add($"A.ManufacturerCode LIKE '%{request.ManufacturerCode}%'");
                }

                string conditionString = string.Join(" AND ", conditions);
                if (!string.IsNullOrEmpty(conditionString))
                {
                    baseListSql += " AND " + conditionString;
                    baseSql += " AND " + conditionString;
                }
                string list = baseListSql;
                string sql = $"{baseSql} ORDER BY A.ManufacturerCode OFFSET (({request.PageIndex} - 1) * {request.PageSize}) ROWS FETCH NEXT {request.PageSize} ROWS ONLY";

                Apipage<pDto> apipage = new Apipage<pDto>();
                apipage.TotalCount = (int)dBHelper.Scalar(list);
                apipage.PageCount = (int)Math.Ceiling(apipage.TotalCount * 1.0 / request.PageSize);
                apipage.List = dBHelper.GetList<pDto>(sql);
                apiResult.Data = apipage;
            }
            catch (Exception ex)
            {
                apiResult.Code = (ApiEnums)Renum.显示失败;
                apiResult.Message = $"查询数据时发生错误: {ex.Message}";
                apiResult.Data = new Apipage<pDto>();
            }

            return Task.FromResult(apiResult);
        }
    }
}
